#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "class.h"

//加载课程信息
void loadCoursesFromFile(struct Course courses[], int *num_courses) {
    FILE *file = fopen("class.txt", "r");
    if (file == NULL) {
        printf("无法打开课程文件。\n");
        return;
    }

    while (fscanf(file, "%d,%[^,],%[^,],%d,%d,%d,%d\n", &courses[*num_courses].code, courses[*num_courses].name,
                  courses[*num_courses].nature, &courses[*num_courses].hours, &courses[*num_courses].credits,
                  &courses[*num_courses].enrolled_students, &courses[*num_courses].max_students) != EOF) {
        (*num_courses)++;
    }
    fclose(file);
}

//保存课程信息
void saveCoursesToFile(struct Course courses[], int num_courses) {
    FILE *file = fopen("class.txt", "w");
    if (file == NULL) {
        printf("无法保存课程文件。\n");
        return;
    }

    for (int i = 0; i < num_courses; i++) {
        fprintf(file, "%d,%s,%s,%d,%d,%d,%d\n", courses[i].code, courses[i].name, courses[i].nature, courses[i].hours,
                courses[i].credits, courses[i].enrolled_students, courses[i].max_students);
    }
    fclose(file);
}

//添加课程信息
void addCourse(struct Course courses[], int *num_courses) {
    if (*num_courses >= 100) {
        printf("课程数量已达上限，无法添加更多课程。\n");
        return;
    }

    printf("请输入课程编号: ");
    scanf("%d", &courses[*num_courses].code);
    printf("请输入课程名称: ");
    scanf(" %[^\n]s", courses[*num_courses].name);
    printf("请输入课程性质: ");
    scanf(" %[^\n]s", courses[*num_courses].nature);
    printf("请输入课程学时: ");
    scanf("%d", &courses[*num_courses].hours);
    printf("请输入课程学分: ");
    scanf("%d", &courses[*num_courses].credits);
    printf("请输入课程人数上限: ");
    scanf("%d", &courses[*num_courses].max_students);
    courses[*num_courses].enrolled_students = 0;
    (*num_courses)++;

    // 打开 class.txt 文件以附加（追加）模式写入课程信息
    FILE *classFile = fopen("class.txt", "a");
    if (classFile == NULL) {
        printf("无法打开课程文件。\n");
        return;
    }

    // 写入课程信息到文件
    fprintf(classFile, "%d,%s,%s,%d,%d,%d,%d\n", courses[*num_courses - 1].code, courses[*num_courses - 1].name,
            courses[*num_courses - 1].nature, courses[*num_courses - 1].hours, courses[*num_courses - 1].credits,
            courses[*num_courses - 1].enrolled_students, courses[*num_courses - 1].max_students);
    fclose(classFile);
    logOperation("新增课程");
}

//显示课程信息
void viewCourseDetails(struct Course courses[], int num_courses, int course_code) {
    for (int i = 0; i < num_courses; i++) {
        if (courses[i].code == course_code) {
            printf("课程编号: %d\n", courses[i].code);
            printf("课程名称: %s\n", courses[i].name);
            printf("课程性质: %s\n", courses[i].nature);
            printf("课程学时: %d\n", courses[i].hours);
            printf("课程学分: %d\n", courses[i].credits);
            printf("已选人数: %d\n", courses[i].enrolled_students);
            printf("人数上限: %d\n", courses[i].max_students);
            return;
        }
    }
    printf("未找到课程编号为 %d 的课程。\n", course_code);
}

//修改课程信息
void modifyCourse(struct Course courses[], int num_courses, int course_code) {
    for (int i = 0; i < num_courses; i++) {
        if (courses[i].code == course_code) {
            printf("请输入新的课程名称: ");
            scanf(" %[^\n]s", courses[i].name);
            printf("请输入新的课程性质: ");
            scanf(" %[^\n]s", courses[i].nature);
            printf("请输入新的课程学时: ");
            scanf("%d", &courses[i].hours);
            printf("请输入新的课程学分: ");
            scanf("%d", &courses[i].credits);
            printf("请输入新的课程已选人数: ");
            scanf("%d", &courses[i].enrolled_students);
            printf("请输入新的课程人数上限: ");
            scanf("%d", &courses[i].max_students);
            saveCoursesToFile(courses, num_courses);
            logOperation("修改课程信息");
            return;
        }
    }
    printf("未找到课程编号为 %d 的课程。\n", course_code);
}

//删除课程信息
void deleteCourse(struct Course courses[], int *num_courses, int course_code) {
    for (int i = 0; i < *num_courses; i++) {
        if (courses[i].code == course_code) {

            // 移除课程并将后续课程向前移动
            for (int j = i; j < (*num_courses - 1); j++) {
                courses[j] = courses[j + 1];
            }
            (*num_courses)--;
            saveCoursesToFile(courses, *num_courses);
            logOperation("删除课程");
            return;
        }
    }
    printf("未找到课程编号为 %d 的课程。\n", course_code);
}

//写入日志
void logOperation(char *operation) {
    FILE *logFile = fopen("log.log", "a");
    if (logFile == NULL) {
        printf("无法打开日志文件。\n");
        return;
    }
    fprintf(logFile, "%s\n", operation);
    fclose(logFile);
}
